package com.zzg.util;

import java.io.InputStream;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;

/**
 * 配置文件获取工具类
 * @author zzg
 *
 */
public class YamlUtil {
	 private YamlUtil(){
	        throw new IllegalStateException("Utility class");
	    }

	    public static Map loadYaml(String fileName){
	        InputStream in = YamlUtil.class.getClassLoader().getResourceAsStream(fileName);
	        return StringUtils.isNotEmpty(fileName) ? new Yaml().load(in) : null;
	    }

	    public static Object getProperty(Map<?, ?> map, Object qualifiedKey) {
	        if (map != null && !map.isEmpty() && qualifiedKey != null) {
	            String input = String.valueOf(qualifiedKey);
	            if (StringUtils.isNotEmpty(input)) {
	                if (input.contains(".")) {
	                    int index = input.indexOf('.');
	                    String left = input.substring(0, index);
	                    String right = input.substring(index + 1);
	                    return getProperty((Map<?, ?>) map.get(left), right);
	                } else{
	                    return map.getOrDefault(input, null);
	                }
	            }
	        }
	        return null;
	    }
}
